Returns the name of a file, a directory, or all of the files and the directories on a drive or in a directory that match the specified search path.
個々のファイルやディレクトリの名前および、指定した検索パスに該当するドライブやディレクトリの中にある、すべてのファイルおよびディレクトリの名前を返します。
Dir [(Text As String) [, Attrib As Integer]]
Dir [(Text As String) [, Attrib As Integer]]
String
文字列
Text: Any string expression that specifies the search path, directory or file. This argument can only be specified the first time that you call the Dir function. If you want, you can enter the path in URL notation.
Text: 検索パス、ディレクトリ、ファイルを指定する文字列表式。この引数を指定できるのは、最初に Dir 関数を呼び出す場合だけです。必要であれば、パス指定にURL 指定を用いることもできます。
Attrib: Any integer expression that specifies bitwise file attributes. The Dir function only returns files or directories that match the specified attributes. You can combine several attributes by adding the attribute values:
Attrib:ファイル属性の指定ビットを示す整数表式。Dir 関数は、指定した属性に該当するファイルやディレクトリドライブのみを返します。こうした属性指定は、該当する値を加算することで、複数の指定を組み合わせることが可能です。
0 : Normal files.
0 : 通常のファイル。
16 : Returns the name of the directory only.
16 : ディレクトリの名前のみを返す。
Use this attribute to check if a file or directory exists, or to determine all files and folders in a specific directory.
この属性指定は、特定のファイルやディレクトリが存在するかをチェックしたい場合や、1 つのディレクトリ中にあるすべてのファイルとフォルダ名を確認する場合などに使用します。
To check if a file exists, enter the complete path and name of the file. If the file or directory name does not exist, the Dir function returns a zero-length string ("").
特定のファイルが存在するかをチェックするには、該当ファイルの絶対パスとファイル名を指定します。該当するファイルやディレクトリが存在しなかった場合、Dir 関数は文字長ゼロの空白文字列 ("") を返します。
To generate a list of all existing files in a specific directory, proceed as follows: The first time you call the Dir function, specify the complete search path for the files, for example, "D:\Files\*.sxw". If the path is correct and the search finds at least one file, the Dir function returns the name of the first file that matches the search path. To return additional file names that match the path, call Dir again, but with no arguments.
指定ディレクトリ中にあるすべてのファイル名を取得するには、下記の手順で操作します。Dir 関数の初回呼び出し時に、検索パスを「D:\Files\*.sxw」のような完全な形で指定します。パスの指定に間違いが無く、該当するファイルが 1 つでも検索された場合、Dir 関数は検索パス上で最初に検出したファイル名を返します。検索パス上に存在する2番目以降のファイル名を得るには、引数を指定せずに Dir 関数を再度呼び出します。
To return directories only, use the attribute parameter. The same applies if you want to determine the name of a volume (for example, a hard drive partition)
ディレクトリ名のみを取得したい場合は、属性指定用のパラメータを使います。ボリューム名を取得する場合も同様です (たとえばハードディスクのパーティションなど)。
Sub ExampleDir
Sub ExampleDir
REM Displays all files and directories
REM すべてのファイルとディレクトリの表示
Dim sPath As String
Dim sPath As String
Dim sDir as String, sValue as String
Dim sDir as String, sValue as String
sDir="Directories:"
sDir="Directories:"
sPath = CurDir
sPath = CurDir
sValue = Dir$(sPath + getPathSeparator + "*",16)
sValue = Dir$(sPath + getPathSeparator + "*",16)
Do
Do
If sValue <> "." and sValue <> ".." Then
If sValue <> "." and sValue <> ".."Then
if (GetAttr( sPath + getPathSeparator + sValue) AND 16) >0 then
if (GetAttr( sPath + getPathSeparator + sValue) AND 16) >0 then
REM get the directories
REM ディレクトリの取得
sDir = sDir & chr(13) & sValue
sDir = sDir & chr(13) & sValue
End If
End If
End If
End If
sValue = Dir$
SValue = Dir$
Loop Until sValue = ""
Loop Until sValue = ""
MsgBox sDir,0,sPath
MsgBox sDir,0,sPath
End sub
End sub